package com.xin.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xin.dao.MemberDao;
import com.xin.pojo.Member;
import com.xin.service.MemberService;
import com.xin.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 夜天
 * @Date 2021/12/19 21:47
 * @Version 1.0.0
 * @description： 处理会员相关操作的服务实现类
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    // 根据手机号查询当前用户是否为会员
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    // 将当前用户信息保存至会员表
    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if(password != null){
            // 使用MD5 给明文算法加密
            password = MD5Utils.md5(password);

        }
        memberDao.add(member);
    }
    // 根据当前月份查询会员数量
    @Override
    public List<Integer> findMemberContByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            // 根据日期统计会员数，统计指定日期 date 之前的会员数
            Integer count = memberDao.findMemberContBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }
}
